package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.BusinessExtrasCodeType;
import com.zjdiepu.www.base.enums.type.BusinessExtrasType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.yw.YwBusinessExtrasMapper;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.ext.YwBusinessExtrasExt;

/**
 * 业务信息_附加信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月19日 18时16分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwBusinessExtrasServiceImpl extends BaseService implements IYwBusinessExtrasService {
	public static final Logger logger = LoggerFactory.getLogger(YwBusinessExtrasServiceImpl.class);

	@Autowired
	private YwBusinessExtrasMapper ywBusinessExtrasMapper;
	
	@Override
	public boolean checkLoanEditLock(String busiId) {
		YwBusinessExtrasExt ywBusinessExtrasExt = new YwBusinessExtrasExt(); 
		ywBusinessExtrasExt.setBaseWhere();
		ywBusinessExtrasExt.setBusiId(busiId);
		ywBusinessExtrasExt.setTypeExtras(BusinessExtrasType.LOCK_STATUS.getCode());
		ywBusinessExtrasExt.setCodeExtras(BusinessExtrasCodeType.LOAN_EDIT_LOCK.getCode());
		ywBusinessExtrasExt.setRows(1);
		List<YwBusinessExtras> ywBusinessExtras = ywBusinessExtrasMapper.selectByExt(ywBusinessExtrasExt);
		if(CollectionUtils.isNotEmpty(ywBusinessExtras)) {
			return "1".equals(ywBusinessExtras.get(0).getValueExtras());
		}
		return false;
	}
	
	@Override
	public void saveUpdateAndDelete(YwBusinessExtras ywBusinessExtras, OperateType operType) {
		switch (operType) {
		case add:
			ywBusinessExtras.initInsertData(ywBusinessExtras.getModifier(), ywBusinessExtras.getModifierName());
			ywBusinessExtrasMapper.insert(ywBusinessExtras);
			break;
		case edit:
			ywBusinessExtras.initUpdateData(ywBusinessExtras.getModifier(), ywBusinessExtras.getModifierName());
			// 影响的记录数
			int updateCount = ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtras);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywBusinessExtras.initDeleteData(ywBusinessExtras.getModifier(), ywBusinessExtras.getModifierName());
			// 影响的记录数
			int deleteCount = ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtras);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwBusinessExtras> selectByExt(YwBusinessExtrasExt ywBusinessExtrasExt) {
		return ywBusinessExtrasMapper.selectByExt(ywBusinessExtrasExt);
	}
	
	@Override
	public int countByExt(YwBusinessExtrasExt ywBusinessExtrasExt){
		return ywBusinessExtrasMapper.countByExt(ywBusinessExtrasExt);
	}
	
	@Override
	public YwBusinessExtras selectById(String id) {
		return ywBusinessExtrasMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywBusinessExtrasMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwBusinessExtras ywBusinessExtras) {
		return ywBusinessExtrasMapper.countRefined(ywBusinessExtras);
	}

	@Override
	public YwBusinessExtras selectRefined(YwBusinessExtras ywBusinessExtras) {
		return ywBusinessExtrasMapper.selectRefined(ywBusinessExtras);
	}
	
	@Override
	public int deletedRefined(YwBusinessExtras ywBusinessExtras) {
		return ywBusinessExtrasMapper.deletedRefined(ywBusinessExtras);
	}

	@Override
	public boolean repeat(YwBusinessExtras ywBusinessExtras) {
		int count = countRefined(ywBusinessExtras);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywBusinessExtras.getId())) {
			return true;
		}
		YwBusinessExtras dbItem = selectRefined(ywBusinessExtras);
		if(null == dbItem) {
			return false;
		}
		return !ywBusinessExtras.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywBusinessExtrasMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwBusinessExtras> ywBusinessExtrass) {
		return ywBusinessExtrasMapper.insertBatch(ywBusinessExtrass);
	}
}
